package com.myfir.action.user;

import com.myfir.action.BaseAction;
import com.myfir.entity.User;
import com.myfir.service.user.UserService;

public class UserRegisterAction extends BaseAction {
	private String password;
	private String name;
	private UserService userService;
	private String passwordconfirm;

	public UserService getUserService() {
		return userService;
	}

	public String getPasswordconfirm() {
		return passwordconfirm;
	}

	public void setPasswordconfirm(String passwordconfirm) {
		this.passwordconfirm = passwordconfirm;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() {
		try {
			User user = userService.findByName(name);
			System.out.println(user==null);
			if(name=="") {
				request.setAttribute("user_register_error1", "username can not be null");
				return "fail";
			}
			if (user != null) {
				request.setAttribute("user_register_error2", "username is already exist");
				return "fail";
			}
			if (!password.equals(passwordconfirm)) {
				request.setAttribute("user_register_error3", "wrong password");
				return "fail";
			}
			if (user == null && password.equals(passwordconfirm)&&name!="") {
				User newuser = new User();
				newuser.setName(name);
				newuser.setPassword(password);
				userService.register(newuser);
				session.put("user", newuser);
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "fail";
	}
}
